package com.jl.js.process.command;

import com.jl.js.console.IConsole;
import com.jl.js.console.LifeCycle;

public class ConsoleCommand implements ICommand {

	private final static String[] cmds = new String[] { "quit", "restart",
			"help", "q" };

	private final static String helpInfo = ""
			+ "Command                Description\n"
			+ "=======                ===========\n"
			+ "help                   Display usage and help messages. \n"
			+ "quit/q                 Quit the shell. \n"
			+ "restart                Restart the shell,all state will be cleared \n"
			+ "						  \n"
			+ "defineClass(className) Define an extension using the Java class\n"
			+ "                       named with the string argument. \n"
			+ "                       Uses ScriptableObject.defineClass().\n"
			+ "load(['foo.js', ...])  Load JavaScript source files named by \n"
			+ "                       string arguments. \n"
			+ "loadClass(className)   Load a class named by a string argument.\n"
			+ "                       The class must be a script compiled to a\n"
			+ "                       class file. \n"
			+ "print([expr ...])      Evaluate and print expressions. \n";

	private String cmd;
	private IConsole console;

	public ConsoleCommand(IConsole console, String cmd) {
		this.cmd = cmd.toLowerCase().trim();
		this.console = console;
	}

	@Override
	public void execute() {
		if (cmd.equals("restart")) {
			console.setState(LifeCycle.RESTART);
			return;
		} else if (cmd.equals("quit") || cmd.equals("q")) {
			console.setState(LifeCycle.QUIT);
			return;
		} else if (cmd.equals("help")) {
			console.getOut().print(helpInfo);
		}
	}

	public static String[] getSupport() {
		return cmds;
	}

	@Override
	public String toString() {
		return cmd;
	}

}
